/**
 * jims
 */
package com.jims.master.modules.sys.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.sys.entity.BaseUnit;
import com.jims.master.modules.sys.entity.TUnitFz;
import com.jims.master.modules.sys.service.BaseUnitService;
import com.jims.master.modules.sys.service.TUnitFzService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 * @author 樊多
 * @version 2017-06-12
 * <p>
 *     合作单位
 * </p>
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/baseUnit")
public class BaseUnitController extends BaseController {

	@Autowired
	private BaseUnitService baseUnitService;
	@Autowired
	private TUnitFzService tUnitFzService;
	
	@ModelAttribute
	public BaseUnit get(@RequestParam(required=false) String id) {
		BaseUnit entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseUnitService.get(id);
		}
		if (entity == null){
			entity = new BaseUnit();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BaseUnit baseUnit, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BaseUnit> page = baseUnitService.findPage(new Page<BaseUnit>(request, response), baseUnit); 
		model.addAttribute("page", page);
		return "modules/sys/baseUnitIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(BaseUnit baseUnit, Model model) {
		model.addAttribute("baseUnit", baseUnit);
		return "modules/sys/baseUnitForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BaseUnit baseUnit, Model model) {
	    StringData data=new StringData();
		int i=baseUnitService.save(baseUnit);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BaseUnit baseUnit) {
	    StringData data=new StringData();
		int i=baseUnitService.delete(baseUnit);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
	/**
	 * @param  baseUnit 传递参数
	 * @return String    返回类型
	 * @throws
	 * @Title: addGroupForm
	 * @Desciption: (跳转到添加分组页面)
	 * @author 樊多
	 * @date 2017-06-12
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "addGroupForm")
	public String addGroupForm(BaseUnit baseUnit, Model model) {
		/**根据诊疗项目查询所有对照**/
//		List<BaseDto> listBaseUnit = baseUnitService.findGroupByUnitId(baseUnit.getId());
		List<BaseUnit> list = baseUnitService.findList(new BaseUnit());
		model.addAttribute("list", list);
		model.addAttribute("baseUnit", baseUnit);
		return "modules/sys/tUnitFzForm";
	}
	/**
	 * 通过单位Id查询分组
	 * @param tUnitFz
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findGroupByUnitId")
	public String findGroupByUnitId(TUnitFz tUnitFz, Model model){
		List<TUnitFz> list=tUnitFzService.findList(tUnitFz);
		model.addAttribute("tUnitFz", list);
		return "modules/sys/selectTUnitFz";
	}
}